/* main.vala
 *
 * Copyright (C) 2008  Frederik 'playya' Sdun
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * Author:
 *     Frederik 'playya' Sdun <Frederik.Sdun@gmail.com>
 */

using GLib;
using Rob;


public class Robd : GLib.Object 
{
    private GLib.List<Rob.Rules.Rule> rules;
    private string confpath;
    construct
    {
        this.rules = new GLib.List<Rob.Rules.Rule>();
    }
    public Robd () 
    {

    }

        
    public void load()
    {
        string home = GLib.Environment.get_home_dir();
        this.confpath = Path.build_filename( home , ".rob/" );
        GLib.Dir dir = null;
        stdout.printf("Loading from: %s\n", confpath);
        try
        {
            dir = GLib.Dir.open(confpath);
        }
        catch ( GLib.FileError e )
        {
            stdout.printf("Could not open %s. Creating\n", confpath);
            GLib.DirUtils.create_with_parents(confpath,644);
            try
            {
                dir = GLib.Dir.open(confpath);
            }
            catch ( GLib.FileError e )
            {
                            error( "out of diskspace or freerunner on fire?" );
                //should never happen
            }

        }
        string file,f;
        for( f = dir.read_name(); f != null ; f = dir.read_name() )
        {
            file = Path.build_filename( this.confpath, f );
            stdout.printf("Loading configfile from %s \n", file);
            Rob.actionfactory.reset(  );
            Rob.rulefactory.reset(  );
            GLib.KeyFile kf = new GLib.KeyFile();
            try
            {
                kf.load_from_file(file,GLib.KeyFileFlags.KEEP_COMMENTS|GLib.KeyFileFlags.KEEP_TRANSLATIONS);
                Rob.Rules.Rule r = ( Rob.Rules.Rule )Rob.rulefactory.create( kf , "root" );
                //r.load(kf);
                this.rules.prepend(r);
            }
            catch ( GLib.KeyFileError e)
            {
                GLib.critical("Could not load config file %s: %s\n",file, e.message);    
            }
            catch( GLib.FileError fe )
            {
                GLib.critical("Could not load file %s: %s",file, fe.message);
            }
        }
        
    }
    public void run()
    {
        foreach (Rob.Rules.Rule r in this.rules )
        {
            r.run();
            GLib.debug("Rule %s started\n",r.title);
        }
        Rob.mainloop.run();
        GLib.debug("MainLoop started\n");
    }

    public void stop()
    {
            
        foreach ( Rob.Rules.Rule r in this.rules )
        {
            r.stop();
            GLib.debug("Ruleset %s stopped\n", r.title);
        }
        Rob.mainloop.quit();
        GLib.debug("MainLoop stopped\n");
    }

    static int main (string[] args) {
        Rob.init(ref args);

        var main = new Robd ();
        main.load();
        main.run ();
        
        return 0;
    }
}


